//
//  CenterViewController.m
//  yameishe1
//
//  Created by 吴坤金 on 2017/2/28.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "CenterViewController.h"
#import "CenterHeaderView.h"
#import "MessageViewController.h"

#import "HomeBottomView.h"
#import "ZFChartRangeView.h"
#import "ZFBarChart.h"
@interface CenterViewController ()<ZFGenericChartDataSource, ZFBarChartDelegate>
{
    HomeBottomView *_bottomView;
    ZFChartRangeView *_rangeView;
    NSMutableArray *_selectArray;
    NSMutableArray *_selectColorArray;
    NSArray *_rangeTitleArray;
    NSDictionary *_rangerDic;
    UIView *_callView;
    
}
@property (nonatomic, strong) ZFBarChart * barChart;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation CenterViewController
#pragma mark 懒加载tableView
//- (UITableView *)tableView {
//    if (!_tableView) {
//        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//        _tableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight-120);
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//        _tableView.rowHeight = 60;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        //        _tableView.backgroundColor = RGBColor(31, 59, 118, 1);
//        // 41, 89 ,180
//        _tableView.backgroundColor = [UIColor yellowColor];
//        CenterHeaderView *centerHeaderView = [CenterHeaderView viewForNib];
//        [centerHeaderView contributionButtonClickBlock:^{
//
//        }];
//        _tableView.tableHeaderView = centerHeaderView;
//        _tableView.tableFooterView = [[UIView alloc] init];
//        
//        // 注册cell
//        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"111"];
//        
//    }
//    return _tableView;
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self controllerSetting];
    
   
    __weak typeof(self) wself = self;
     self.navigationController.navigationBar.translucent = YES;
    // Do any additional setup after loading the view.
    
    _rangeTitleArray = [[NSArray alloc]initWithObjects:@"直接访问",@"邮件营销",@"联盟广告",@"视频广告",@"搜索引擎",@"百度",@"谷歌",@"必应",@"其他", nil];
    _rangerDic = [[NSDictionary alloc]initWithObjectsAndKeys:
                  @{@"red": @"193",@"green": @"48",@"blue": @"45"  },@"直接访问",
                  @{@"red": @"48",@"green": @"69",@"blue": @"84"  },@"邮件营销",
                  @{@"red": @"99",@"green": @"161",@"blue": @"168"  },@"联盟广告",
                  @{@"red": @"211",@"green": @"129",@"blue": @"102"  },@"视频广告",
                  @{@"red": @"145",@"green": @"200",@"blue": @"175"  },@"搜索引擎",
                  @{@"red": @"145",@"green": @"160",@"blue": @"132"  },@"百度",
                  @{@"red": @"200",@"green": @"133",@"blue": @"43"  },@"谷歌",
                  @{@"red": @"188",@"green": @"162",@"blue": @"154"  },@"必应",
                  @{@"red": @"110",@"green": @"112",@"blue": @"116"  },@"其他", nil];

    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(leftClick)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"消息" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick)];
    self.navigationItem.rightBarButtonItem = rightButton;
    self.title = @"首页";
//    [self.view addSubview:self.tableView];

    _bottomView = [[[NSBundle mainBundle]loadNibNamed:@"HomeBottomView" owner:self options:nil] firstObject];
    [self.view addSubview:_bottomView];
    
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-50);
        make.height.equalTo(@(90));
    }];
    [_bottomView setCallbackDidSelect:^(NSNumber *tag) {
        [wself onTouchBottom:tag];
    }];
    
    [self setRangeView];
    [self setZFChartView];
    [self setCallView];

}
-(void)setCallView
{
    _callView = [[[NSBundle mainBundle]loadNibNamed:@"space" owner:self options:nil] firstObject];
    _callView.frame = self.view.bounds;
    _callView.alpha = 0;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_callView];
    UIButton *callBtn = [_callView viewWithTag:101];
    [callBtn addTarget:self action:@selector(submitPhoneCall) forControlEvents:UIControlEventTouchUpInside];
    UIButton *cancleBtn = [_callView viewWithTag:102];
    [cancleBtn addTarget:self action:@selector(hideCallView) forControlEvents:UIControlEventTouchUpInside];
    UIView *bgView = [_callView viewWithTag:1000];
    bgView.layer.cornerRadius=4.5;
}
-(void)onTouchBottom:(NSNumber*)tag
{
    switch ([tag intValue]) {
        case 0:
        {
        
        }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                _callView.alpha = 1;
            } completion:nil];
        }
            break;
            
        default:
            break;
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.view layoutIfNeeded];
    self.tabBarController.tabBar.hidden=NO;
//    self.hidesBottomBarWhenPushed = YES;
    
}
-(void)setRangeView
{
    _rangeView = [[[NSBundle mainBundle]loadNibNamed:@"ZFChartRangeView" owner:self options:nil] firstObject];
    [self.view addSubview:_rangeView];
    [_rangeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(_bottomView.mas_top);
        make.height.equalTo(@(120));
    }];
    [_rangeView setCallbackDidSelect:^(NSNumber *tag, NSArray *selectArray) {
        _selectArray = [[NSMutableArray alloc]init];
//        NSArray *tempArray;
        for (int i = 0; i<_rangeTitleArray.count; i++) {
            NSString *str = _rangeTitleArray[i];
            if ([selectArray containsObject:str]) {
                [_selectArray addObject:str];
                NSDictionary *colordic = [_rangerDic objectForKey:str];
                float red = [[NSNumber numberWithString:colordic[@"red"]]floatValue ];
                float green = [[NSNumber numberWithString:colordic[@"green"]]floatValue ];
                float blue = [[NSNumber numberWithString:colordic[@"blue"]]floatValue ];
                [_selectColorArray addObject:RGBColor(red,green,blue)];
            }
        }
    }];

}
-(void)setZFChartView
{
    self.barChart = [[ZFBarChart alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-90-200-64-100)];
    self.barChart.dataSource = self;
    self.barChart.delegate = self;
    self.barChart.topicLabel.text = @"投放分布柱状图";
    self.barChart.unit = @"";
    self.barChart.isShowYLineSeparate = YES;
    self.barChart.isShowXLineSeparate = YES;
    
    self.barChart.valueType = kValueTypeDecimal;
    self.barChart.numberOfDecimal = 2;
    
    [self.barChart strokePath];
    [self.view addSubview:self.barChart];
    [self.barChart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(_rangeView.mas_top);
        make.top.equalTo(self.view).offset(200);
    }];


}

#pragma mark 导航栏左按钮点击事件
- (void)leftClick {
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
#pragma mark 导航栏右按钮点击事件
- (void)rightClick {
   MessageViewController *messageVC = [[MessageViewController alloc] init];
    [self.navigationController pushViewController:messageVC animated:YES];
    
}


-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111" forIndexPath:indexPath];
    
    cell.textLabel.text = @"111";
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}


#pragma mark - ZFGenericChartDataSource

- (NSArray *)valueArrayInGenericChart:(ZFGenericChart *)chart{
    return @[@[@"123", @"300", @"490", @"380", @"167", @"235", @"235"],//->代表一个类在不同横坐标的值
             @[@"256", @"283", @"236", @"240", @"183", @"200", @"235"],
             @[@"123", @"300", @"490", @"380", @"167", @"235", @"235"],
             @[@"256", @"283", @"236", @"240", @"183", @"200", @"235"],
             @[@"123", @"300", @"490", @"380", @"167", @"235", @"235"],
             @[@"256", @"283", @"236", @"240", @"183", @"200", @"235"],
//             @[@"123", @"300", @"490", @"380", @"167", @"235", @"235"],
//             @[@"256", @"283", @"236", @"240", @"183", @"200", @"235"],
             @[@"256", @"256", @"256", @"256", @"256", @"256", @"235"]];
}

- (NSArray *)nameArrayInGenericChart:(ZFGenericChart *)chart{
    return @[@"周一", @"周二", @"周三", @"周四", @"周五", @"周六",@"周日"];
}

- (NSArray *)colorArrayInGenericChart:(ZFGenericChart *)chart{
    return @[ZFColor(71, 204, 255, 1), ZFGold, ZFColor(16, 140, 39, 1),ZFColor(71, 204, 255, 1), ZFGold, ZFColor(16, 140, 39, 1),ZFColor(71, 204, 255, 1), ZFGold, ZFColor(16, 140, 39, 1)];
//    return _selectColorArray;
}

- (CGFloat)axisLineMaxValueInGenericChart:(ZFGenericChart *)chart{
    return 1800;
}

//- (CGFloat)axisLineMinValueInGenericChart:(ZFGenericChart *)chart{
//    return 100;
//}

- (NSUInteger)axisLineSectionCountInGenericChart:(ZFGenericChart *)chart{
    return 10;
}

#pragma mark - ZFBarChartDelegate

- (CGFloat)barWidthInBarChart:(ZFBarChart *)barChart{
    return 20.f;
}

- (CGFloat)paddingForGroupsInBarChart:(ZFBarChart *)barChart{
    return 15.f;
}

- (CGFloat)paddingForBarInBarChart:(ZFBarChart *)barChart{
    return 3.f;
}

- (id)valueTextColorArrayInBarChart:(ZFBarChart *)barChart{
    return ZFBlue;
    //    return @[ZFColor(71, 204, 255, 1), ZFColor(253, 203, 76, 1), ZFColor(16, 140, 39, 1)];
}

- (NSArray<ZFGradientAttribute *> *)gradientColorArrayInBarChart:(ZFBarChart *)barChart{
    //该组第1个bar渐变色
    ZFGradientAttribute * gradientAttribute1 = [[ZFGradientAttribute alloc] init];
    gradientAttribute1.colors = @[(id)ZFColor(71, 204, 255, 1).CGColor, (id)ZFWhite.CGColor];
    gradientAttribute1.locations = @[@(0.5), @(0.99)];
    gradientAttribute1.startPoint = CGPointMake(0, 0.5);
    gradientAttribute1.endPoint = CGPointMake(1, 0.5);
    
    //该组第2个bar渐变色
    ZFGradientAttribute * gradientAttribute2 = [[ZFGradientAttribute alloc] init];
    gradientAttribute2.colors = @[(id)ZFGold.CGColor, (id)ZFWhite.CGColor];
    gradientAttribute2.locations = @[@(0.5), @(0.99)];
    gradientAttribute2.startPoint = CGPointMake(0, 0.5);
    gradientAttribute2.endPoint = CGPointMake(1, 0.5);
    
    //该组第3个bar渐变色
    ZFGradientAttribute * gradientAttribute3 = [[ZFGradientAttribute alloc] init];
    gradientAttribute3.colors = @[(id)ZFColor(16, 140, 39, 1).CGColor, (id)ZFWhite.CGColor];
    gradientAttribute3.locations = @[@(0.5), @(0.99)];
    gradientAttribute3.startPoint = CGPointMake(0, 0.5);
    gradientAttribute3.endPoint = CGPointMake(1, 0.5);
    
    return [NSArray arrayWithObjects:gradientAttribute1, gradientAttribute2, gradientAttribute3,gradientAttribute1, gradientAttribute2, gradientAttribute3,gradientAttribute1, nil];
}

- (void)barChart:(ZFBarChart *)barChart didSelectBarAtGroupIndex:(NSInteger)groupIndex barIndex:(NSInteger)barIndex bar:(ZFBar *)bar popoverLabel:(ZFPopoverLabel *)popoverLabel{
    //特殊说明，因传入数据是3个subArray(代表3个类型)，每个subArray存的是6个元素(代表每个类型存了1~6年级的数据),所以这里的groupIndex是第几个subArray(类型)
    //eg：三年级第0个元素为 groupIndex为0，barIndex为2
    NSLog(@"第%ld个颜色中的第%ld个",(long)groupIndex,(long)barIndex);
    
    //可在此处进行bar被点击后的自身部分属性设置
    //    bar.barColor = ZFDeepPink;
    //    bar.isAnimated = YES;
    //    bar.opacity = 0.5;
    //    [bar strokePath];
    
    //可将isShowAxisLineValue设置为NO，然后执行下句代码进行点击才显示数值
    //    popoverLabel.hidden = NO;
}

- (void)barChart:(ZFBarChart *)barChart didSelectPopoverLabelAtGroupIndex:(NSInteger)groupIndex labelIndex:(NSInteger)labelIndex popoverLabel:(ZFPopoverLabel *)popoverLabel{
    //理由同上
    NSLog(@"第%ld组========第%ld个",(long)groupIndex,(long)labelIndex);
    
    //可在此处进行popoverLabel被点击后的自身部分属性设置
    //    popoverLabel.textColor = ZFSkyBlue;
    //    [popoverLabel strokePath];
}

-(void)submitPhoneCall
{
    UITextField *tf = [_callView viewWithTag:101];
}
-(void)hideCallView
{
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        _callView.alpha = 0;
    } completion:nil];
}
@end
